package com.ty.xiaoyu.service.impl;

import com.ty.xiaoyu.entity.Comment;
import com.ty.xiaoyu.entity.Reply;
import com.ty.xiaoyu.exception.CommentOperationException;
import com.ty.xiaoyu.exception.ReplyOperationException;
import com.ty.xiaoyu.service.CommentService;
import com.ty.xiaoyu.util.MyTimeUtil;
import com.ty.xiaoyu.util.RandomIdUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl extends BaseService implements CommentService {
    @Override
    public List<Comment> getCommentListByReplyId(String replyId) throws CommentOperationException {
//        List<Comment> commentList = commentMapper.queryCommentListByReplyId(replyId);
//        if (commentList == null || commentList.isEmpty()) {
//            throw new CommentOperationException("未找到评论");
//        }
//        return commentList;
        return commentMapper.queryCommentListByReplyId(replyId);
    }

    @Override
    public Comment addComment(Comment comment) throws CommentOperationException {
        comment.setCommentId(RandomIdUtil.getId());
        comment.setCommentTime(MyTimeUtil.nowTime());
        comment.setCommentState(1);
        Integer state = commentMapper.insertComment(comment);
        if (state <= 0) {
            throw new CommentOperationException("评论失败");
        }
        return comment;
    }

    @Override
    public Comment getCommentById(String commentId) {
        Comment comment = commentMapper.queryCommentById(commentId);
        if (comment == null) {
            throw new CommentOperationException("未找到评论");
        }
        return comment;
    }
}
